package com.sugon.controller.module;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.dao.ModuleSeriesDao;
import com.sugon.entity.ModuleArchivesEntity;
import com.sugon.entity.ModuleSeriesEntity;
import com.sugon.modules.mold.consts.MoldState;
import com.sugon.service.ModuleArchivesService;
import com.sugon.service.ModuleSeriesService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 模具机型Controller
 *
 * @author sugon
 * @date 2019-08-19 11:47:01
 */
@Controller
@RequestMapping("moduleseries")
public class ModuleSeriesController {

    @Resource
    private ModuleSeriesService moduleSeriesService;
    @Resource
    private ModuleSeriesDao moduleSeriesDao;
    @Resource
    private ModuleArchivesService moldService;


    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("moduleseries:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<ModuleSeriesEntity> moduleSeriesList = moduleSeriesService.queryList(query);
        int total = moduleSeriesService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(moduleSeriesList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("moduleseries:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        ModuleSeriesEntity moduleSeries = moduleSeriesService.queryObject(id);

        return R.ok().put("moduleSeries", moduleSeries);
    }

    /**
     * 保存
     */
    @AutoLog("模具机型--保存")
    @RequestMapping("/save")
    @RequiresPermissions("moduleseries:save")
    @ResponseBody
    public R save(@RequestBody ModuleSeriesEntity moduleSeries) {
        ModuleSeriesEntity moduleSeriesInDB = moduleSeriesDao.loadByCode(moduleSeries.getModelCode());
        if (moduleSeriesInDB != null) {
            return R.error("模具机型编号已存在,请确认");
        }
        return moduleSeriesService.save(moduleSeries) == 1 ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @AutoLog("模具机型--修改")
    @RequestMapping("/update")
    @RequiresPermissions("moduleseries:update")
    @ResponseBody
    public R update(@RequestBody ModuleSeriesEntity moduleSeries) {
        moduleSeriesService.update(moduleSeries);

        return R.ok();
    }

    /**
     * 删除
     */
    @AutoLog("模具机型--删除")
    @RequestMapping("/delete")
    @RequiresPermissions("moduleseries:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        moduleSeriesService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {
        List<ModuleSeriesEntity> moduleSeriesList = moduleSeriesService.queryList(params);
        List<ModuleArchivesEntity> moldList = moldService.queryList("");
        Map<String, List<ModuleArchivesEntity>> productModelGroup = moldList.stream().collect(Collectors.groupingBy(ModuleArchivesEntity::getProductModel));
        for (ModuleSeriesEntity moduleSeries : moduleSeriesList) {
            int totalNum = 0;
            long availableNum = 0;
            List<ModuleArchivesEntity> matchedMoldList = productModelGroup.get(moduleSeries.getModelCode());
            if (CollectionUtil.isNotEmpty(matchedMoldList)) {
                totalNum = matchedMoldList.size();
                availableNum = matchedMoldList.stream().filter(mold -> !CollectionUtil.newArrayList(MoldState.MUJUBAOFEI.getState(), MoldState.XIANGKUAIBAOFEI.getState(), MoldState.FENGCUN.getState(), MoldState.WAIJIE.getState()).contains(mold.getCurrentState())).count();
            }
            moduleSeries.setComplexName(StrUtil.format(moduleSeries.getModelName() + " {}/{}", availableNum, totalNum));
        }
        return R.ok().put("list", moduleSeriesList);
    }
}
